package teams;


import java.lang.reflect.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author denaas
 */
public class Reflectable {
    
    private List fields;
    
    public Reflectable() {
        fields = new LinkedList();
        Class objClass = this.getClass();
        while (!objClass.equals(Reflectable.class)) {
            fields.addAll(Arrays.asList(objClass.getDeclaredFields()));
            objClass = objClass.getSuperclass();
        }        
    }
    
    public List getAttrsNames() {
        List result = new LinkedList();
        for (int i=0; i < fields.size(); i++) {
            Field field = (Field)fields.get(i);
            result.add(field.getName());
        }
        return result;
    }
    
    public static List getAttrsNamesFromClass(Class theClass) {
        List fs = new LinkedList();
        fs.addAll(Arrays.asList(theClass.getDeclaredFields()));
        
        List result = new LinkedList();
        for (int i=0; i < fs.size(); i++) {
            Field field = (Field)fs.get(i);
            result.add(field.getName());
        }
        return result;
    }
    
    private Field findFieldByName(String name)
    {
        if (name == null)
            return null;
        for (int i=0; i < fields.size(); i++) {
            Field field = (Field)fields.get(i);
            if ( field.getName().equals(name)) {
                return field;
            }
        }
        return null;
    }
    
    public Object getAttrValue(String name) {
        if (name == null)
            return null;
        Field field = findFieldByName(name);
        if (field != null) {
            try {                    
                boolean tmp = field.isAccessible();
                field.setAccessible(true);
                String res = field.get(this).toString();
                field.setAccessible(tmp);
                return res;
            } catch (Exception ex) {
                return null;
            }
        }
        return null;
    }
    
    public String getAttrValueToStr(String name) {
        if (name == null)
            return "NULL";
        Object value = getAttrValue(name);
        if (value == null) {
            return "NULL";
        }
        return value.toString();
    }
    
    public Class getAttrValueType(String name) {
        if (name == null)
            return null;
        return findFieldByName(name).getType();
    }
    
    public void setAttrValue(String name, Object val) {
        if (name == null || val == null)
            return;
        Field field = findFieldByName(name);
        try {
            boolean tmp = field.isAccessible();
            field.setAccessible(true);
            field.set(this, val);
            field.setAccessible(tmp);
            return;
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Reflectable.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Reflectable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
    